<!-- eslint-disable no-unused-expressions -->
<template>
  <div>
    <!-- <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="date" label="日期" width="180"> </el-table-column>
      <el-table-column prop="name" label="姓名" width="180"> </el-table-column>
      <el-table-column prop="address" label="地址"> </el-table-column>
    </el-table> -->
    <el-table
      :data="
        tableData
          ? tableData.filter(
              (data) =>
                !search ||
                data.name.toLowerCase().includes(search.toLowerCase()) ||
                data.date.toLowerCase().includes(search.toLowerCase()) ||
                data.address.toLowerCase().includes(search.toLowerCase())
            )
          : tableData
      "
      style="width: 100%"
    >
      <el-table-column label="日期" prop="date"> </el-table-column>
      <el-table-column label="姓名" prop="name"> </el-table-column>
      <el-table-column label="地址" prop="address"> </el-table-column>
      <el-table-column align="right">
        <template v-slot:header="scope">
          <el-input
            v-model="search"
            size="mini"
            placeholder="输入关键字搜索"
            @click="scope.$index, scope.row"
          />
        </template>
        <template v-slot="scope">
          <div class="editButton">
            <el-button
              size="mini"
              type="primary"
              @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </div>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      title="编辑信息"
      v-model="dialogFormVisible"
      style="text-align: left"
    >
      <el-form :model="form">
        <el-form-item label="姓名" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="日期" :label-width="formLabelWidth">
          <el-input v-model="form.date" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="地址" :label-width="formLabelWidth">
          <el-input v-model="form.address" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-radio v-model="form.ridio" label="0">正常</el-radio>
          <el-radio v-model="form.ridio" label="1">停用</el-radio>
        </el-form-item>
      </el-form>
      <div class="dialog-footer" style="text-align: right">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="editData()">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 第二个表格 -->
    <el-input
      v-model="inputvalue"
      size="mini"
      placeholder="请输入姓名"
      style="width: 200px; margin-left: 15px; margin-right: 15px"
    />
    <el-select clearable v-model="addressValue" placeholder="请选择">
      <el-option
        v-for="item in fileterData"
        :key="item.id"
        :label="item.address"
        :value="item.address"
      >
      </el-option>
    </el-select>
    <el-select clearable v-model="statusValue" placeholder="请选择">
      <el-option
        v-for="item in fileterData"
        :key="item.id"
        :label="item.status.code === '0' ? '正常' : '停用'"
        :value="item.status.code === '0' ? '正常' : '停用'"
      >
      </el-option>
    </el-select>
    <el-button type="primary" @click="Search" style="margin-left: 10px">
      <Search
        style="width: 1.3em; height: 1.3em; margin-right: 8px; margin-top: -2px"
      />搜索
    </el-button>
    <el-button type="primary" @click="reset">
      <Refresh
        style="width: 1.3em; height: 1.3em; margin-right: 8px; margin-top: -2px"
      />重置
    </el-button>

    <el-table
      ref="multipleTable"
      :data="tableDataB"
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column label="日期">
        <template v-slot="scope">{{ scope.row.date }}</template>
      </el-table-column>
      <el-table-column prop="name" label="姓名"> </el-table-column>
      <el-table-column prop="address" label="地址" show-overflow-tooltip>
      </el-table-column>
      <el-table-column label="状态" show-overflow-tooltip>
        <template v-slot="scope">
          <div class="name-wrapper">
            <el-tag
              size="medium"
              :type="scope.row.status.code === '0' ? '' : 'danger'"
              >{{ getCategoryName(scope.row.status.code) }}</el-tag
            >
          </div>
        </template>
      </el-table-column>
      <el-table-column align="right">
        <template v-slot="scope">
          <div class="editButton">
            <el-button
              size="mini"
              type="primary"
              @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </div>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { getList } from '@/api/table'
import { Search, Refresh } from '@element-plus/icons-vue'
export default {
  components: {
    Search,
    Refresh
  },
  data () {
    return {
      tableData: [],
      tableDataB: [],
      fileterData: [],
      tableDataA: [],
      listLoading: true,
      search: '',
      row: {},
      addressValue: '',
      statusValue: '',
      inputvalue: '',
      dialogFormVisible: false,
      multipleSelection: [],
      form: {
        name: '',
        date: '',
        address: '',
        ridio: '0',
        status: {
          code: ''
        }
      },
      formLabelWidth: '120px'
    }
  },
  created () {
    this.getListData()
  },
  mounted () {
  },
  methods: {
    uniques (arr) {
      const res = new Map()
      return arr.filter((arr) => !res.has(arr.address) && res.set(arr.address, 1))
    },
    unStatus (arr) {
      const res = new Map()
      return arr.filter((item) => !res.has(item.status.code) && res.set(item.status.code, 1))
    },
    getListData () {
      getList('/data.json').then(
        (res) => {
          this.tableData = res.data
          this.tableDataB = res.data
          this.tableDataA = res.data

          this.tableData = this.fileterData = this.uniques(this.tableDataB)
          const newData = []
          this.tableData.forEach((item, index) => {
            newData.push(Object.assign({}, item, { img: index }))
          })
          this.tableData = newData
          console.log(this.tableData, 'new')
          // this.tableData = this.fileterData = this.unStatus(this.tableDataB)
          // console.log(this.fileterData)
          // this.unque(this.tableDataB)
          // this.listLoading = false
        },
        (error) => {
          console.log(error)
        }
      )
    },
    getCategoryName (val) {
      if (!val) {
        return ''
      }
      for (let i = 0; i < this.tableDataB.length; i++) {
        const item = this.tableDataB[i]
        // console.log(item, 111)
        if (item.status.code === val) {
          return item.status.name
        }
      }
    },
    // unque (arr) {
    //   var obj = {}
    //   arr = arr.reduce(function (item, next) {
    //     // console.log(obj, 111)
    //     // console.log(item, 'item')
    //     // console.log(next, 'next')

    //     // eslint-disable-next-line no-unused-expressions
    //     obj[next.address] ? '' : obj[next.address] = true && item.push(next)

    //     // const changeStatus = item.status === '0' ? '正常' : '停用'
    //     // obj[changeStatus] ? '' : obj[changeStatus] = true
    //     return item
    //   }, [])
    //   this.fileterData = arr
    // },
    handleEdit (index, row) {
      this.row = row
      this.dialogFormVisible = true
      this.form = row
      this.form.ridio = row.status.code
      this.getCategoryName(row.status.code)
      console.log(row, 'row')
      // console.log(this.form, 111)
      // this.form.status.code = row.status.code
      // this.radio = row.status
      // console.log(this.form)
    },
    handleDelete (index, row) {
      // 设置类似于console类型的功能
      this.$confirm('永久删除该数数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          // 移除对应索引位置的数据，可以对row进行设置向后台请求删除数据
          this.tableData.splice(index, 1)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    editData () {
      // console.log(this.form, 'form')
      // const res = this.getCategoryName(this.form.status.code)
      // console.log(res, 'res')
      // this.form = this.tableDataB
      // console.log(this.form.ridio, 11)
      // console.log(this.form.ridio, 111)
      this.row.status.code = this.row.ridio
      this.row.status.name = this.form.ridio === '0' ? '正常' : '停用'
      // console.log(this.row.status.name)
      this.row = this.form
      // console.log(this.row, 'this.row')
      // this.row = this.form
      // // this.row.status.name = this.getCategoryName(this.form.status.code)
      // console.log(this.row, this.row.status.name)
      // console.log(this.getCategoryName(this.row.status.code))
      // this.form = this.tableDataB
      // this.getCategoryName(this.form.forEach(item => {
      //   return item.status.code
      // }))
      // console.log(this.getCategoryName(this.form.foreach(item => {
      //   return item.status.code
      // })), 3333)
      // this.tableDataB = this.form

      // res = this.tableDataB
      // for (let i = 0; i < this.tableDataB.length; i++) {
      //   this.tableDataB[i].status = this.form.radio
      // }
      // this.tableDataB.forEach(item => {
      //   item.status = this.form.radio
      // })
      // this.tableDataB.status = this.form.radio
      // console.log(this.form, 'form')
      // console.log(this.tableData, 'tableData')

      // this.form.radio = this.tableData.status
      this.$message({
        type: 'success',
        message: '修改成功!'
      })
      // sessionStorage.setItem('tableData', JSON.stringify(this.form))
      this.dialogFormVisible = false
    },
    handleSelectionChange (val) {
      this.multipleSelection = val
    },
    Search () {
      this.fileterData = this.tableDataB
      const _search = this.inputvalue.toLowerCase()
      const _address = this.addressValue
      const _stauts = this.statusValue
      const newListData = []

      if (_search) {
        this.fileterData.filter((item) => {
          if (item.name.toLowerCase().indexOf(_search) !== -1) {
            newListData.push(item)
          }
        })
        this.tableDataB = newListData
        this.inputvalue = ''
        this.fileterData = this.tableData
        // eslint-disable-next-line no-unused-expressions
      } else if (_address) {
        this.fileterData.filter((item) => {
          if (item.address === _address) {
            newListData.push(item)
          }
        })
        this.tableDataB = newListData
        // this.fileterData = this.uniques(this.tableDataB)
        // this.fileterData = this.tableData
        // this.unque(this.tableDataB)
        this.addressValue = ''
        this.fileterData = this.tableData
      } else if (_stauts) {
        this.fileterData.filter((item) => {
          const changeStatus = item.status.code === '0' ? '正常' : '停用'
          if (changeStatus === _stauts) {
            newListData.push(item)
          }
        })
        this.tableDataB = newListData
        // this.fileterData = this.tableData
        // this.unque(this.tableDataB)
        this.statusValue = ''
        this.fileterData = this.tableData
      } else {
        this.tableDataB = this.tableDataA
        this.fileterData = this.tableData
        // this.fileterData = this.tableDataB
        // this.unque(this.tableDataB)
      }
    },
    reset () {
      this.inputvalue = ''
      this.addressValue = ''
      this.statusValue = ''
      this.Search()
      this.tableDataB = this.tableDataA
      this.fileterData = this.tableData
      // this.fileterData = this.tableDataB
      // this.unque(this.tableDataB)
    }
  }
}
</script>

<style scoped lang="scss">
.editButton {
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
